import React,{Component} from 'react';
import {View,Text,Image,StyleSheet,FlatList} from 'react-native';
const http_url =
  "https://raw.githubusercontent.com/facebook/react-native/0.51-stable/docs/MoviesExample.json";
export default class Movie extends Component{
  constructor(props) {
    super(props);
    this.state = {
      data: [],
      loaded: false
    };

  }

  componentDidMount() {
    fetch(http_url)
      .then(response => response.json())
      .then(responseData => {
        console.log(responseData);
        this.setState({
          data: this.state.data.concat(responseData.movies),
          loaded: true
        });
      });
  }
  render(){
    if (!this.state.loaded) {
      return this.renderLoadingView();
    }

    return(
      <View style={styles.container}>
        {/* 电影界面滚动栏 */}
        <FlatList
          data={this.state.data}
          renderItem={this.renderMovie}
          style={styles.list}
          keyExtractor={item => item.id}/>

      </View>
    )
  }

  renderLoadingView() {
    return (
      <View style={styles.container}>
        <Text>Loading movies...</Text>
      </View>
    );
  }

  renderMovie({ item }) {
    return (
      <View style={styles.container}>
        <Image
          source={{ uri: item.posters.thumbnail==="" ? 'icon_movie' : item.posters.thumbnail }}
          style={styles.thumbnail}
        />
        <View style={styles.rightContainer}>
          <Text style={styles.title}>{item.title}</Text>
          <Text style={styles.year}>{item.year}</Text>
        </View>
      </View>
    );
  }
}
const styles=StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: "row",
    justifyContent: "center",
    alignItems: "center",
    backgroundColor: "#FFF",

  },
  rightContainer: {
    flex: 1
  },
  title: {
    fontSize: 20,
    marginBottom: 8,
    textAlign: "center"
  },
  year: {
    textAlign: "center"
  },
  thumbnail: {
    width: 53,
    height: 81
  },
  list: {
    paddingTop: 20,
    backgroundColor: "#FFF",
  }
})
